<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\Article;

use app\massage\model\ArticleList;
use app\massage\model\Cap;
use app\massage\model\Coach;
use app\massage\model\CoachTimeList;
use app\massage\model\Commission;
use app\massage\model\CommShare;
use app\massage\model\Date;

use app\massage\model\Order;
use app\massage\model\OrderAddress;
use app\massage\model\OrderGoods;
use app\massage\model\RefundOrder;
use app\massage\model\SubData;
use app\massage\model\SubList;
use app\massage\model\User;
use app\massage\model\Wallet;
use app\massage\model\WorkLog;
use longbingcore\wxcore\Excel;
use think\App;
use app\massage\model\Order as Model;
use think\facade\Db;


class AdminExcel extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    protected $attendant_name;

    protected $comm_share_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

        $this->comm_share_model   = new CommShare();

        $this->attendant_name = getConfigSetting($this->_uniacid,'attendant_name');

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //手机号搜索
        if(!empty($input['mobile'])){

            $order_address_model = new OrderAddress();

            $order_address_dis[] = ['mobile','like','%'.$input['mobile'].'%'];

            $order_id = $order_address_model->where($order_address_dis)->column('order_id');

            $dis[] = ['a.id','in',$order_id];
        }

        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','in',$this->admin_arr];
        }

        //合伙人
        if(!empty($input['partner_id'])){

            $dis[] = ['a.partner_id','=',$input['partner_id']];

        }

        if(!empty($input['admin_id'])){

            $dis[] = ['a.admin_id','=',$input['admin_id']];
        }

        if(!empty($input['pay_type'])){
            //订单状态搜索
            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }else{
            //除开待转单
            $dis[] = ['a.pay_type','<>',8];

        }

        $map = [];
        //店铺名字搜索
        if(!empty($input['coach_name'])){

            $map[] = ['b.coach_name','like','%'.$input['coach_name'].'%'];

            $map[] = ['d.now_coach_name','like','%'.$input['coach_name'].'%'];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['channel_cate_id'])){

            $dis[] = ['e.cate_id','=',$input['channel_cate_id']];

        }

        if(!empty($input['channel_name'])){

            $dis[] = ['e.user_name','like','%'.$input['channel_name'].'%'];
        }

        if(!empty($input['is_channel'])){

            $dis[] = ['a.pay_type','>',1];

            $dis[] = ['a.channel_id','<>',0];

        }
        //是否是加钟
        if(isset($input['is_add'])){

            $dis[] = ['a.is_add','=',$input['is_add']];

        }

        if(!empty($input['is_coach'])){

            if($input['is_coach']==2){

                $dis[] = ['a.coach_id','=',0];
            }else{

                $dis[] = ['a.coach_id','>',0];

            }

        }

        if(!empty($input['is_store'])){

            if($input['is_store']==1){

                $dis[] = ['a.store_id','>',0];
            }else{

                $dis[] = ['a.store_id','=',0];

            }

        }

        $data = $this->model->adminDataSelect($dis,$map,$this->_user['phone_encryption']);

        if(!empty($input['is_channel'])){

            if(!empty($input['is_add'])){

                $name = '渠道财务加单';

                $type = 2;

            }else{

                $name = '渠道财务订单';

                $type = 1;

            }

        }else{

            if(!empty($input['is_add'])&&$input['is_add']==1){

                $name = '加单列表';

                $type = 3;

            }else{

                $name = '订单列表';

                $type = 2;

            }

        }

        $attendant_name = $this->attendant_name;

        $header[] = '订单ID';
        $header[] = '服务项目';
        $header[] = '项目价格';
        $header[] = '项目数量';
        $header[] = '物料费';
        $header[] = '下单人';
        $header[] = '手机号';
        $header[] = $attendant_name;
        if(empty($input['is_add'])){
             $header[] = $attendant_name.'类型';
        }

        if(!empty($input['is_channel'])){

            $header[] = '渠道商';

            $header[] = '渠道';
        }

        $header[] = '服务方式';

        $header[] = '服务开始时间';

        if(empty($input['is_add'])){

            $header[] = '出行费用';
        }

        $header[] = '服务项目费用';

        if(empty($input['is_add'])){

            $header[] = '实收金额';
        }
        $header[] = '退款金额';

        if(empty($input['is_add'])){

            $header[] = '子订单号';

        }else{

            $header[] = '主订单号';

        }

        $header[] = '系统订单号';
        $header[] = '付款订单号';
        $header[] = '代理商';
        $header[] = '下单时间';
        $header[] = '支付方式';
        $header[] = '状态';

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['goods_name'];

            $info[] = $v['price'];

            $info[] = $v['num'];

            $info[] = $v['init_material_price'];

            $info[] = $v['user_name'];

            $info[] = $v['mobile'];

            $info[] = !empty($v['coach_info']['coach_name'])?$v['coach_info']['coach_name']:'';

            if(empty($input['is_add'])){

                $info[] = $v['coach_id']>0?'入驻'.$attendant_name:'非入驻'.$attendant_name;
            }

            if(!empty($input['is_channel'])){

                $info[] = $v['channel_name'];

                $info[] = $v['channel'];
            }

            $info[] = $v['store_id']>0?'到店服务':'上门服务';

            $info[] = date('Y-m-d H:i:s',$v['start_time']);

            if(empty($input['is_add'])) {

                $info[] = $v['car_price'];

            }

            $info[] = $v['init_service_price'];

            if(empty($input['is_add'])) {

                $info[] = $v['pay_price'];
            }

            $info[] = $v['refund_price'];

            if(empty($input['is_add'])){

                $info[] = !empty($v['add_order_id'][0]['order_code'])?$v['add_order_id'][0]['order_code']:'';

            }else{

                $info[] = !empty($v['add_pid']['order_code'])?$v['add_pid']['order_code']:'';;

            }

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = !empty($v['partner_id'])?$v['partner_name']:$v['admin_name'];

            $info[] = date('Y-m-d H:i:s',$v['create_time']);

            $info[] = $this->payModel($v['pay_model']);

            $info[] = $this->orderStatusText($v['pay_type']);

            $new_data[] = $info;
        }


        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data,'',$type);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-12-26 21:59
     * @功能说明:支付方式
     */
    public function payModel($type){

        switch ($type){

            case 1;

                $text = '微信支付';
                break;

            case 2;

                $text = '余额支付';
                break;
            case 3;

                $text = '支付宝支付';
                break;

            default:

                $text = $type;
                break;

        }

        return $text;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:32
     * @功能说明:
     */
    public function orderStatusText($status){

        $attendant_name = $this->attendant_name;

        switch ($status){

            case 1:
                return '待支付';

                break;
            case 2:
                return '待服务';

                break;
            case 3:
                return $attendant_name.'接单';

                break;
            case 4:
                return $attendant_name.'出发';

                break;
            case 5:
                return $attendant_name.'到达';

                break;
            case 6:
                return '服务中';

                break;

            case 7:
                return '已完成';

                break;
            case 8:
                return '待转单';

                break;

            case -1:
                return '已取消';

                break;

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2022-12-15 12:05
     * @功能说明:提交内容导出
     */
    public function subDataList(){

        $input = $this->_param;

        $article_model = new ArticleList();

        $sub_list_model= new SubList();

        $sub_data_model= new SubData();

        $article_title = $article_model->where(['id'=>$input['article_id']])->value('title');
        //获取导出标题
        $title_data = $article_model->getFieldTitle($input['article_id']);

        $title = ['用户ID','微信昵称'];

        $title = array_merge($title,array_column($title_data,'title'));

        $title[] = '提交时间';

        $name  = '文章表单数据导出-'.$article_title;

        $diss[] = ['article_id','=',$input['article_id']];

        $diss[] = ['status','=',1];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $diss[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        if(!empty($input['id'])){

            $diss[] = ['id','in',$input['id']];

        }

        $list = $sub_list_model->where($diss)->order('id desc')->select()->toArray();

        $new_data = [];

        if(!empty($list)){

            $user_model = new User();

            foreach ($list as &$v){

                $user_info = $user_model->where(['id'=>$v['user_id']])->field('nickName,avatarUrl')->find();

                $info   = array();

                $info[] = $v['user_id'];

                $info[] = $user_info['nickName'];

                if(!empty($title_data)){

                    foreach ($title_data as $vs){

                        $dis = [

                            'field_id' => $vs['field_id'],

                            'sub_id'   => $v['id']
                        ];

                        $find = $sub_data_model->where($dis)->value('value');;

                        $info[] = !empty($find)?$find:'';
                    }
                }

                $info[] = date('Y-m-d H:i:s',$v['create_time']);

                $new_data[] = $info;

            }

        }

        $excel = new Excel();

        $excel->excelExport($name,$title,$new_data);

        return $this->success(true);

    }




    /**
     * @author chenniang
     * @DataTime: 2023-04-18 11:31
     * @功能说明:财务报表
     */
    public function financeDetailedList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $where[] = ['b.uniacid','=',$this->_uniacid];

        $dis[] = ['pay_type','=',7];

        $where[] = ['b.pay_type','=',7];

        if(!empty($input['order_code'])){

            $dis[] = ['order_code','like','%'.$input['order_code'].'%'];

            $where[] = ['b.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

//            $dis[] = ['can_tx_date','between',"{$input['start_time']},{$input['end_time']}"];
//
//            $where[] = ['b.can_tx_date','between',"{$input['start_time']},{$input['end_time']}"];

            $dis[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

            $where[] = ['b.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        if($this->_user['is_admin']==0){

            $dis[] = ['admin_id','in',$this->admin_arr];

            $where[] = ['b.admin_id','in',$this->admin_arr];
        }

        $type = !empty($input['type'])?$input['type']:0;

        if(!empty($input['top_name'])){

            $id = $this->model->getFinanceOrderId($input['top_name'],$type);

            $dis[] = ['id','in',$id];

            $where[] = ['b.id','in',$id];
        }

        if(!empty($input['type'])){

            $id = $this->model->getTypeCommOrder($input['type']);

            $dis[] = ['id','in',$id];

            $where[] = ['b.id','in',$id];

        }

        $data = $this->model->financeDetailedSelect($dis);

        $total_list = $this->model->financeDetailedDataV2($dis,$where);

        $last[] = '合计:';
        $last[] = '';
        $last[] = '';
        $last[] = '';
        $last[] = '';
        $last[] = $total_list['true_service_price'];
        $last[] = '';
        $last[] = $total_list['coach_cash'];
        $last[] = $total_list['car_price'];
        if(in_array($this->_user['city_type'],[3])||$this->_user['is_admin']!=0){
            $last[] = '';
            $last[] = $total_list['province_cash'];
        }
        if(in_array($this->_user['city_type'],[3,1])||$this->_user['is_admin']!=0) {
            $last[] = '';
            $last[] = $total_list['city_cash'];
        }
        $last[] = '';
        $last[] = $total_list['district_cash'];
        $last[] = '';
        $last[] = $total_list['user_cash'];
        $last[] = '';
        $last[] = $total_list['partner_cash'];
        $last[] = '';
        $last[] = $total_list['salesman_cash'];
        $last[] = '';
        $last[] = $total_list['channel_cash'];
        if($this->_user['is_admin']!=0){

            $last[] = $total_list['remain_cash'];
        }

        $name = '财务报表';

        $header[] = '系统订单号';
        $header[] = '付款订单号';
        $header[] = '下单时间';
        $header[] = '入账账号';
        $header[] = '财务类型';
        $header[] = '支付金额';
        $header[] = $this->attendant_name.'名称';
        $header[] = $this->attendant_name.'提成';
        $header[] = $this->attendant_name.'车费';
        if(in_array($this->_user['city_type'],[3])||$this->_user['is_admin']!=0){
            $header[] = '省代名称';
            $header[] = '省代提成';
        }
        if(in_array($this->_user['city_type'],[3,1])||$this->_user['is_admin']!=0) {
            $header[] = '市代名称';
            $header[] = '市代提成';
        }
        $header[] = '区代名称';
        $header[] = '区代提成';
        $header[] = '分销员姓名';
        $header[] = '分销提成';
        $header[] = '经纪人姓名';
        $header[] = '经纪人提成';
        $header[] = '业务员姓名';
        $header[] = '业务员提成';
        $header[] = '渠道商姓名';
        $header[] = '渠道商提成';
        if($this->_user['is_admin']!=0){

            $header[] = '平台利润';
        }
        $new_data = [];

        if(!empty($data)){

            foreach ($data as $v){

                $info   = array();

                $info[] = $v['order_code']."\t";

                $info[] = $v['transaction_id']."\t";

                $info[] = date('Y-m-d H:i:s',$v['end_time']);

                $info[] = $this->payModel($v['pay_model']);

                $info[] = $v['is_add']==1?'加钟':'销售';

                $info[] = $v['true_service_price'];

                $info[] = !empty($v['coach_name'])?$v['coach_name']:'';

                $info[] = !empty($v['coach_cash'])?$v['coach_cash']:0;

                $info[] = $v['car_price'];

                if(in_array($this->_user['city_type'],[3])||$this->_user['is_admin']!=0) {

                    $info[] = !empty($v['province_name']) ? $v['province_name'] : '';

                    $info[] = $v['province_cash'];
                }

                if(in_array($this->_user['city_type'],[3,1])||$this->_user['is_admin']!=0) {

                    $info[] = $v['city_name'];

                    $info[] = $v['city_cash'];
                }

                $info[] = $v['district_name'];

                $info[] = $v['district_cash'];

                $info[] = $v['cash_user_name'];

                $info[] = $v['user_cash'];

                $info[] = $v['partner_name'];

                $info[] = $v['partner_cash'];

                $info[] = $v['salesman_name'];

                $info[] = $v['salesman_cash'];

                $info[] = $v['channel_name'];

                $info[] = $v['channel_cash'];

                if($this->_user['is_admin']!=0) {

                    $info[] = $v['remain_cash'];
                }

                $new_data[] = $info;
            }

        }

        if(!empty($new_data)){

            $new_data[]=$last;
        }

        $excel = new Excel();

        $excel->excelCsv($name,$header,$new_data);

        $this->controlActionLog(0);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-08-28 23:03
     * @功能说明:佣金记录
     */
    public function commList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.cash','>',0];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }else{

            $dis[] = ['a.status','>',-1];

        }

        $where = [];

        if(!empty($input['top_name'])){

            $where[] = ['c.nickName','like','%'.$input['top_name'].'%'];

            $where[] = ['e.agent_name','like','%'.$input['top_name'].'%'];

            $where[] = ['f.coach_name','like','%'.$input['top_name'].'%'];

            $where[] = ['g.now_coach_name','like','%'.$input['top_name'].'%'];

            $where[] = ['h.user_name','like','%'.$input['top_name'].'%'];

            $where[] = ['m.user_name','like','%'.$input['top_name'].'%'];
        }

        if($this->_user['is_admin']==0){

            $dis[] = ['a.admin_id','in',$this->admin_arr];

        }

        if(!empty($input['type'])){

            if($input['type']==2){

                $dis[] = ['a.type','in',[2,5,6]];
            }else{

                $dis[] = ['a.type','=',$input['type']];
            }

        }

        if(!empty($input['order_code'])){

            $dis[] = ['d.order_code','=',$input['order_code']];

        }

        $comm_model = new Commission();

        $user_model = new User();

        $data = $comm_model->recordSelect($dis,$where);

        $commission_custom = getConfigSetting($this->_uniacid,'commission_custom');

        if(!empty($data)){

            foreach ($data as &$v){

                $v['nickName'] = $user_model->where(['id'=>$v['user_id']])->value('nickName');

                $v['coach_cash_control'] = $v['status']==2&&$v['admin_id']==0&&in_array($v['type'],[3,8])&&$v['top_id']==0?1:0;

                $v['cash'] = round($v['cash'] ,2);

                if($v['car_price']>0){

                    $v['pay_price'] = $v['pay_price'].'含车费'.$v['car_price'];
                }
                if($v['material_price']>0){

                    $v['pay_price'] = $v['pay_price'].' 含物料费'.$v['material_price'];
                }

                if(in_array($v['type'],[2,5,6,11])){

                    $v['top_name'] = $v['admin_name'];

                }elseif (in_array($v['type'],[3,8])){

                    $v['top_name'] = $v['coach_name'];

                    if($v['top_id']==0&&$v['car_cash']>0){

                        $v['cash'] = $v['cash'].'含车费'.$v['car_cash'];

                    }

                    if(empty($v['top_id'])){

                        $v['top_name'] = $v['now_coach_name'];
                    }

                }elseif ($v['type']==10){

                    $v['top_name'] = $v['channel_name'];

                }elseif ($v['type']==12){

                    $v['top_name'] = $v['salesman_name'];
                }

                if($v['type']==2){

                    if($commission_custom==0){

                        $v['balance'] = '平台抽成-'.$v['balance'];
                    }

                    $coach_cash = $v['coach_cash']>0?'包含'.$v['coach_cash'].'线下服务费，':'';

                    $car_cash   = $v['car_cash']>0?'包含'.$v['car_cash'].'线下车费':'';

                    $v['cash'] = !empty($coach_cash)||!empty($car_cash)?$v['cash'].'('.$coach_cash.$car_cash.')':$v['cash'];
                }

                $share_cash = 0;
                //技师查询是否有分摊金额
                if($v['type']==3){

                    $share_cash = $this->comm_share_model->where(['order_id'=>$v['order_id'],'type'=>1])->sum('share_cash');
                }
                //代理商分摊金额
                if(in_array($v['type'],[2,5,6])){

                    $share_cash = $this->comm_share_model->where(['order_id'=>$v['order_id'],'type'=>2,'share_id'=>$v['top_id']])->sum('share_cash');
                }

                if(!empty($share_cash)){

                    $v['cash'] .= '  被分摊金额:'.$share_cash.'元';
                }

            }

        }

        $name = '分销佣金';

        $header=[
            '用户ID',
            '佣金获得者',
            '来源',
            '系统订单号',
            '付款订单号',
            '佣金类型',
            '状态',
            '提成比例',
            '订单总金额',
            '此单提成金额',
            '时间',
        ];

        $new_data =[];

        if(!empty($data)){

            foreach ($data as $vs){

                $info   = array();

                $info[] = $vs['id'];

                $info[] = $vs['top_name'];

                $info[] = $vs['nickName'];

                $info[] = $vs['order_code']."\t";

                $info[] = $vs['transaction_id']."\t";

                $info[] = $this->getCommType($vs['type']);

                $info[] = $vs['status']==2?'已到账':'未到账';

                if($vs['type']==1){

                    if(!empty($vs['order_goods'])){

                        $str = '';

                        foreach ($vs['order_goods'] as $vss){

                            $str .= $vss['goods_name'].'比例'.$vss['balance'].'%,';

                        }
                    }

                    $info[] = $str;

                }else{

                    $info[] = $vs['balance'].'%';

                }

                $info[] = $vs['pay_price'];

                $info[] = $vs['cash'];

                $info[] = date('Y-m-d H:i:s',$vs['create_time']);

                $new_data[] = $info;
            }
        }

        $excel = new Excel();

        $excel->excelCsv($name,$header,$new_data);

        $this->controlActionLog(0);
    }


    /**
     * @author chenniang
     * @DataTime: 2023-04-21 17:28
     * @功能说明:获取佣金类型
     */
    public function getCommType($type){

        $name = $this->attendant_name;

        $arr = [

            1 =>'分销商',

            2 =>'代理商',

            3 => $name,

            5 => '代理商',

            6 => '代理商',

            7 => $name.'拉用户充值余额',

            8 => '车费',

            9 => $name.'经纪人',

            10 => '渠道商',

            11 => '平台',

            12 => '业务员',

        ];

        return $arr[$type];

    }



    /**
     * @author chenniang
     * @DataTime: 2023-06-14 18:28
     * @功能说明:技师业绩统计
     */
    public function coachDataList(){

        $input = $this->_param;

        $input = $this->_param;

        switch ($input['top_type']){
            case 1:

                $top = 'service_timelong';

                break;

            case 2:

                $top = 'service_timelong';

                break;
            case 3:

                $top = 'order_service_price';

                break;
            case 4:

                $top = 'add_balance';

                break;

            case 5:

                $top = 'coach_integral';

                break;

            case 6:

                $top = 'coach_star';

                break;
        }

        if($input['time_type']==1){

            $start_time = strtotime(date('Y-m-d'));

            $end_time   = $start_time+86400;

        }elseif($input['time_type']==2){

            $start_time = strtotime('this week Monday');

            $end_time   = strtotime('this week Sunday');
        }elseif ($input['time_type']==3){

            $start_time = strtotime(date('Y-m-01 00:00:00'));

            $end_time   = time();

        }elseif ($input['time_type']==4){

            $start_time = strtotime('01/01 00:00:00');

            $end_time   = time();
        }else{
            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

        }

        $order_model = new Order();

        $log_model   = new WorkLog();

        $dis = [

            'uniacid' => $this->_uniacid,

            'status' => 2,

        ];

        $where = [];

        if($this->_user['is_admin']==0){

            $where[] = ['admin_id','in',$this->admin_arr];
        }

        $coach_mdoel = new Coach();

        $time_list_model = new CoachTimeList();

        $data = $coach_mdoel
            ->where($dis)
            ->where($where)
            ->field('id as coach_id,coach_name,work_img,user_id')
            ->select()
            ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){
                //服务时长
                $v['service_timelong']    = $order_model->where(['pay_type'=>7,'coach_id'=>$v['coach_id']])->whereBetween("create_time","$start_time,$end_time")->sum('true_time_long');

                $v['order_service_price'] = $order_model->where(['pay_type'=>7,'coach_id'=>$v['coach_id']])->whereBetween("create_time","$start_time,$end_time")->sum('true_service_price');

                $v['add_service_price']   = $order_model->where(['pay_type'=>7,'coach_id'=>$v['coach_id'],'is_add'=>1])->whereBetween("create_time","$start_time,$end_time")->sum('true_service_price');

                $v['order_service_price'] = round($v['order_service_price'],2);

                $v['add_service_price']   = round($v['add_service_price'],2);
                //积分
                $v['coach_integral']      = Db::name('massage_integral_list')->where(['coach_id'=>$v['coach_id'],'type'=>0,'status'=>1])->whereBetween("create_time","$start_time,$end_time")->sum('integral');

                $v['coach_star']          = Db::name('massage_service_order_comment')->where(['coach_id'=>$v['coach_id']])->where('status','>',-1)->whereBetween("create_time","$start_time,$end_time")->avg('star');

                $v['coach_star']          = !empty($v['coach_star'])?$v['coach_star']:5;

                $v['add_balance']         = $v['order_service_price']>0?$v['add_service_price']/$v['order_service_price']:0;

                $v['add_balance']        = round($v['add_balance']*100,2);

                $v['coach_level']        = $coach_mdoel->getCoachLevel($v['coach_id'],$this->_uniacid);

                $v['total_order_count']  = $order_model->where(['coach_id'=>$v['coach_id']])->where('pay_time','>',0)->whereBetween("create_time","$start_time,$end_time")->count();

                if(!empty($v['user_id'])){

                    $v['coach_onlinetime'] = $log_model->where(['coach_id'=>$v['coach_id']])->whereBetween("create_time","$start_time,$end_time")->sum('time');

                    $rest_time = $time_list_model->where(['coach_id'=>$v['coach_id'],'status'=>0,'is_click'=>1,'is_work'=>1])->where('time_str','<',time())->where('time_str','between',"$start_time,$end_time")->field('SUM(time_str_end-time_str) as time_long')->find();

                    $rest_time = $rest_time->time_long;

                    $v['coach_onlinetime'] = floor(($v['coach_onlinetime']-$rest_time)/60);

                    $v['coach_onlinetime'] = $v['coach_onlinetime']>0?$v['coach_onlinetime']:0;
                }else{

                    $v['coach_onlinetime'] = 0;
                }

                $v['service_order_count']= $order_model->where(['coach_id'=>$v['coach_id']])->where('pay_type','>',1)->whereBetween("create_time","$start_time,$end_time")->count();

                $v['cancel_order_count'] = $order_model->where(['coach_id'=>$v['coach_id']])->where('coach_refund_time','>',0)->whereBetween("create_time","$start_time,$end_time")->count();

                $v['refund_balance']     = $v['total_order_count']>0?round(($v['total_order_count']-$v['service_order_count'])/$v['total_order_count']*100,2):0;
            }
        }

        $data = arraySort($data,$top,'desc');

        $name = $this->attendant_name.'数据';

        $header=[
            'ID',
            '姓名',
            '评分',
            '等级',
            '服务时长',
            '在线时长',
            '业绩',
            '加钟率',
            '积分',
            '总订单量',
            '已服务单量',
            '退单率',
            '总拒单量',
        ];

        $new_data =[];

        if(!empty($data)){

            foreach ($data as $vs){

                $info   = array();

                $info[] = $vs['coach_id'];

                $info[] = $vs['coach_name'];

                $info[] = $vs['coach_star'];

                $info[] = !empty($vs['coach_level']['title'])?$vs['coach_level']['title']:'';

                $info[] = $vs['service_timelong'].'分钟';

                $info[] = $vs['coach_onlinetime'].'分钟';

                $info[] = $vs['order_service_price'];

                $info[] = $vs['add_balance'].'%';

                $info[] = $vs['coach_integral'];

                $info[] = $vs['total_order_count'];

                $info[] = $vs['service_order_count'];

                $info[] = $vs['refund_balance'].'%';

                $info[] = $vs['cancel_order_count'];

                $new_data[] = $info;
            }
        }

        $excel = new Excel();

        $excel->excelCsv($name,$header,$new_data);

        $this->controlActionLog(0);
    }
















}
